package com.superquan.selfstudybootmybatis.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.superquan.selfstudybootmybatis.service.SmsService;
import com.superquan.selfstudybootmybatis.utils.email.SmsUtil;
import com.superquan.selfstudybootmybatis.utils.encrypt.md5.IdUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zwq
 * @description 短信业务处理
 * @date 2022/3/29
 */
@Service("smsService")
public class SmsServiceImpl implements SmsService {
    @Resource
    SmsUtil smsUtil;

    @Override
    public Map<String, Object> sendMsg(String phone) {
        Map<String,Object> map = new HashMap<>();
        try {
            //生成验证码并指定长度
            long code = IdUtil.nextId();
            //多个变量
            //String TemplateParam = "{\"name\":\""+name+"\",\"time\":\""+time+"\"}";
            String TemplateParam = "{\"code\":\""+code+"\"}";
            // 短信模板id
//            String TemplateCode = "SMS_231445844";
            //发送短信
            SendSmsResponse response = smsUtil.sendSms(phone,TemplateParam);
            map.put("phone",phone);
            map.put("verifyCode",code);
            if( response.getCode().equals("OK")) {
                map.put("isOk","发送成功");
            }else {
                map.put("isOk","发送失败");
            }
            return map;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
